﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace GestorEventos.Business
{
    public class Parserwebfonte
        {
            private String url;
            private String urlbio;

            public Parserwebfonte()
            {
                this.url = @"http://www.allaboutjazz.com/php/musician_center.php";
                this.urlbio = @"http://www.allaboutjazz.com/php/musician.php?id=";

            }

            public Parserwebfonte(String arg)
            {
                this.url = arg;
                this.urlbio = @"http://www.allaboutjazz.com/php/musician.php?id=";
            }

            public void getFonte()
            {
                string arquivo = @"Data.txt";
                TextWriter ficheiro = new StreamWriter(arquivo);
                StringBuilder sb = new StringBuilder();

                byte[] buf = new byte[8192];

                // ligaçao
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);

                // executa o pedido
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    count = resStream.Read(buf, 0, buf.Length);

                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        sb.Append(tempString);
                    }
                }
                while (count > 0);

                ficheiro.Write(sb.ToString());
                ficheiro.Close();
            }

            public void getBio(String id, String nome)
            {
                string arquivo = nome + ".txt";
                TextWriter ficheiro = new StreamWriter(arquivo);
                StringBuilder sb = new StringBuilder();

                byte[] buf = new byte[8192];

                // ligaçao
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.urlbio + id);

                // executa o pedido
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    count = resStream.Read(buf, 0, buf.Length);

                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        sb.Append(tempString);
                    }
                }
                while (count > 0);

                ficheiro.Write(sb.ToString());
                ficheiro.Close();
            }

        }
}

